package com.zllkj.mm.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.zllkj.mm.MyApp

class BaseDataBindingAdapter<T> : RecyclerView.Adapter<BaseDataBindingAdapter.BindingHolder> {
    protected var context: Context
    protected var inflater: LayoutInflater? = null
    protected var layoutId = 0
    protected var variableId = 0
    protected var list: List<T>? = null
    protected var onRecyclerViewItemClickListener: OnRecyclerViewItemClickListener? = null
    protected var onRecyclerViewItemLongClickListener: OnRecyclerViewItemLongClickListener? = null

    constructor(context: Context) {
        this.context = context
    }

    constructor(context: Context, layoutId: Int, list: List<T>?, resId: Int) {
        this.context = context
        this.layoutId = layoutId
        this.list = list
        variableId = resId
        inflater = MyApp.getInstance().applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingHolder {
        val binding = DataBindingUtil.inflate<ViewDataBinding>(
                LayoutInflater.from(parent.context),
                layoutId,
                parent,
                false)
        val holder = BindingHolder(binding.root, onRecyclerViewItemClickListener, onRecyclerViewItemLongClickListener)
        holder.binding = binding
        return holder
    }

    override fun onBindViewHolder(holder: BindingHolder, position: Int) {
        holder.binding!!.setVariable(variableId, list!![position])
        holder.binding!!.executePendingBindings()
    }

    override fun getItemCount(): Int {
        return list!!.size
    }

    class BindingHolder(itemView: View, itemClick: OnRecyclerViewItemClickListener?, itemLongClick: OnRecyclerViewItemLongClickListener?) : RecyclerView.ViewHolder(itemView), View.OnClickListener, View.OnLongClickListener {
        var binding: ViewDataBinding? = null
        protected var itemClick: OnRecyclerViewItemClickListener?
        protected var itemLongClick: OnRecyclerViewItemLongClickListener?
        override fun onClick(v: View) {
            if (itemClick != null) {
                itemClick!!.onItemClick(adapterPosition, v)
            }
        }

        override fun onLongClick(v: View): Boolean {
            return if (itemLongClick != null) {
                itemLongClick!!.onItemLongClick(adapterPosition, v)
            } else false
        }

        init {
            itemView.setOnClickListener(this)
            itemView.setOnLongClickListener(this)
            this.itemClick = itemClick
            this.itemLongClick = itemLongClick
        }
    }

    interface OnRecyclerViewItemClickListener {
        fun onItemClick(position: Int, view: View?)
    }

    interface OnRecyclerViewItemLongClickListener {
        fun onItemLongClick(position: Int, view: View?): Boolean
    }
}